package com.example.back.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.back.common.Result;
import com.example.back.entity.ExpertGroup;
import com.example.back.entity.SchoolGroup;
import com.example.back.entity.User;
import com.example.back.service.ExpertGroupService;
import com.example.back.service.SchoolGroupService;
import com.example.back.service.UserService;
import com.sun.javafx.geom.transform.Identity;
import org.apache.logging.log4j.message.ReusableMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Jm
 * @date 2024/7/18
 */
@RestController
@RequestMapping("/schoolGroup")
public class SchoolGroupController
{
    @Autowired
    private UserService userService;

    @Autowired
    private SchoolGroupService schoolGroupService;

    @Autowired
    private ExpertGroupService expertGroupService;

    @GetMapping("/selectPage")
    public Result selectByPage(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam String identity)
    {
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        queryWrapper.eq("identity", identity);

        Page<User> page = userService.page(new Page<>(pageNum, pageSize), queryWrapper);

        return Result.success(page);
    }

    @PostMapping("/add")
    public Result add(@RequestBody SchoolGroup schoolGroup)
    {
        schoolGroup.setStatus(0);
        schoolGroupService.save(schoolGroup);
        return Result.success();
    }


    @PostMapping("/getBySchool")
    public Result getBySchool(@RequestBody User user)
    {
        int userId = user.getId();

        QueryWrapper<SchoolGroup> queryWrapper = new QueryWrapper<SchoolGroup>();
        queryWrapper.eq("user_id", userId);

        return Result.success(schoolGroupService.list(queryWrapper));
    }

    @PostMapping("/del")
    public Result delGroup(@RequestBody SchoolGroup schoolGroup)
    {
        schoolGroupService.removeById(schoolGroup.getId());
        return Result.success();
    }

    @GetMapping("/selectSchool/{id}")
    public Result selectSchool(@PathVariable Integer id)
    {
        QueryWrapper<ExpertGroup> groupQueryWrapper = new QueryWrapper<ExpertGroup>();
        groupQueryWrapper.eq("expert_id", id);
        List<ExpertGroup> expertGroups = expertGroupService.list(groupQueryWrapper);
        List<SchoolGroup> list = new ArrayList<>();
        for(ExpertGroup expertGroup : expertGroups)
        {
            QueryWrapper<SchoolGroup> queryWrapper = new QueryWrapper<SchoolGroup>();
            queryWrapper.eq("status", 0);
            queryWrapper.eq("group_id", expertGroup.getId());
            list.addAll(schoolGroupService.list(queryWrapper));
        }
        return Result.success(list);
    }
}
